package com.chen.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.dto.AllProductDto;
import com.chen.entity.Product;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.entity.ProductFullReduction;
import com.chen.entity.ProductLadder;
import com.chen.entity.Skus;
import com.chen.mapper.*;
import com.chen.service.ProductService;
import com.chen.vo.CustomException;
import com.chen.vo.MyProductsVo;
import com.chen.vo.ProductDetailVo;
import com.chen.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author cws
 * @since 2024-11-22
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {


    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductFullReductionMapper productFullReductionMapper;

    @Resource
    private ProductLadderMapper productLadderMapper;

    @Resource
    private BrandsMapper brandsMapper;
    @Resource
    private RegionsMapper regionMapper;

    @Resource
    private SkusMapper skusMapper;



    @Override
    public List<MyProductsVo> myProducts(String type) {
        List<MyProductsVo> myProductsVo = new ArrayList<>();
        if (type.equals("new")) {
            myProductsVo = productMapper.myProductsByNew();
        } else if (type.equals("hot")) {
            myProductsVo = productMapper.myProductsByHot();
        }
        return myProductsVo;
    }

    @Override
    public Result allProduct(AllProductDto allProductDto) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        Page<Product> page = new Page<>(allProductDto.getPage(), allProductDto.getPageSize());
        if (StrUtil.isNotEmpty(allProductDto.getSearchWord())) {
            wrapper.like(Product::getName, allProductDto.getSearchWord());
        }
        if (!Objects.isNull(allProductDto.getRegionId())) {
            wrapper.eq(Product::getRegionId, allProductDto.getRegionId());
        }
//        asc
        if (!Objects.isNull(allProductDto.getType()) && allProductDto.getType().equals(1)) {
            wrapper.orderByAsc(Product::getPrice);
        } else if (!Objects.isNull(allProductDto.getType()) && allProductDto.getType().equals(2)) {
            wrapper.orderByDesc(Product::getPrice);
        }
        if (!Objects.isNull(allProductDto.getStartPrice()) && !Objects.isNull(allProductDto.getEndPrice())) {
            wrapper.ge(Product::getPrice, allProductDto.getStartPrice());
            wrapper.le(Product::getPrice, allProductDto.getEndPrice());
        }
        wrapper.eq(Product::getVerifyStatus, 1);
        wrapper.eq(Product::getStatus,"available");
        Page<Product> pages = productMapper.selectPage(page, wrapper);
        List<Product> records = pages.getRecords();
        List<MyProductsVo> collect = records.stream().map(item -> {
            MyProductsVo myProductsVo = new MyProductsVo();
            myProductsVo.setId(item.getId());
            myProductsVo.setName(item.getName());
            myProductsVo.setImage(item.getDefaultImg());
            myProductsVo.setOriginalPrice(item.getPromotionPrice());
            myProductsVo.setCurrentPrice(item.getPrice());
            return myProductsVo;
        }).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", collect);
        map.put("total", pages.getTotal());
        map.put("page", allProductDto.getPage());
        map.put("pageSize", allProductDto.getPageSize());
        return Result.success(map);
    }

    @Override
    public ProductDetailVo detail(Long id) {
        ProductDetailVo productDetailVo = new ProductDetailVo();
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getId, id);
        wrapper.eq(Product::getVerifyStatus, 1);
        wrapper.eq(Product::getIsDelete, 0);
        wrapper.eq(Product::getStatus, "available");
        Product product = baseMapper.selectOne(wrapper);
        if (product == null) {
            throw new CustomException(400, "商品不存在");
        }
        productDetailVo.setProduct(product);
//       获取阶梯
        if (product.getPromotionType().equals(1l)) {
            LambdaQueryWrapper<ProductLadder> ladderWrapper = new LambdaQueryWrapper<>();
            ladderWrapper.eq(ProductLadder::getProductId, id);
            List<ProductLadder> productLadders = productLadderMapper.selectList(ladderWrapper);
            productDetailVo.setProductLadder(productLadders);
        } else if (product.getPromotionType().equals(2l)) {//满减
            LambdaQueryWrapper<ProductFullReduction> fullWrapper = new LambdaQueryWrapper<>();
            fullWrapper.eq(ProductFullReduction::getProductId, id);
            List<ProductFullReduction> productFullReductions = productFullReductionMapper.selectList(fullWrapper);
            productDetailVo.setProductFullReduction(productFullReductions);
        }

//       获取品牌
        productDetailVo.setBrand(brandsMapper.selectById(product.getBrandId()));
//       获取地区
        productDetailVo.setRegion(regionMapper.selectById(product.getRegionId()));
//        获取sku
        List<Skus> skuses=skusMapper.selectSkusByProductId(id);
        productDetailVo.setSkus(skuses);

        return productDetailVo;
    }
}
